layui.use(['element','laydate','form'], function(){

    var laydate = layui.laydate;
    laydate.render({
        elem: '#birthday'
        ,value: new Date(vm.clientBirthday)
        ,done: function(value, date, endDate){
            var birthday=new Date()
            birthday.setFullYear(date.year,date.month,date.date)
            vm.clientBirthday =birthday
        }
    });

    var form = layui.form;
    setTimeout(function (){
        form.render('select');
    }, 500);
    form.on('select(clientGender)', function(data){
        vm.clientGender = data.value;
    });
    form.on('select(clientMaritalStatus)', function(data){
        vm.clientMaritalStatus = data.value;
    });

});

var vm = new Vue({
    el: '#app',
    data: {
        clientId : "",
        clientName : "",
        clientGender :"",
        clientIdCard :"",
        clientBirthday: "",
        clientPhone: "",
        clientEmail:"",
        clientAddress:"",
        clientMaritalStatus:"",
        clientPrincipal:"",
        userList:[],
        MaritalStatus:['已婚','未婚','保密'],
        Gender:['男','女']
    },
    created() {
        this.getClientInfo()
    },
    methods: {
        getClientInfo(){
            var clientId = location.hash.substring(1);
            $.ajax({
                type: "get",
                url: "/copClient/"+clientId,
                headers: { 'Content-Type': 'application/json;charset=utf-8' },
                dataType: 'json',
                async: true,
                data: {},
                success: function (result) {
                    vm.clientId= result.data.clientId,
                    vm.clientName= result.data.clientName,
                    vm.clientGender= result.data.clientGender,
                    vm.clientBirthday= result.data.clientBirthday,
                    vm.clientPhone= result.data.clientPhone,
                    vm.clientEmail= result.data.clientEmail,
                    vm.clientAddress= result.data.clientAddress,
                    vm.clientMaritalStatus= result.data.clientMaritalStatus,
                    vm.clientPrincipal= result.data.clientPrincipal
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    if(xhr.status==403) {
                        alert("无权限")
                        location.reload([true])
                    }
                }
            });
        },
        update() {
            var tmp = {
                'clientId': vm.clientId,
                'clientName': vm.clientName,
                'clientGender': vm.clientGender,
                'clientBirthday': vm.clientBirthday,
                "clientPhone": vm.clientPhone,
                "clientEmail": vm.clientEmail,
                'clientAddress': vm.clientAddress,
                'clientMaritalStatus': vm.clientMaritalStatus,
                'clientPrincipal': vm.clientPrincipal,
            };
            var data = JSON.stringify(tmp)
            $.ajax({
                type: "put",
                url: "/copClient",
                headers: { 'Content-Type': 'application/json;charset=utf-8' },
                dataType: 'json',
                async: true,
                data: data,
                success: function (result) {
                    console.log(result)
                    if (result.code == 500) {
                        layer.msg(result.msg)
                    } else {
                        alert("成功")
                        window.location.href="client.html"
                    }
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    if(xhr.status==403) {
                        alert("无权限")
                        location.reload([true])
                    }
                }
            });
        },
        back(){
            window.location.href="client.html"
        }
    }
})